package com.zhan.ielts.web.controller;

import com.zhan.frame.core.dao.BaseService;
import com.zhan.frame.core.web.controller.BaseControllerImpl;
import com.zhan.frame.core.web.domain.Result;
import com.zhan.frame.core.web.domain.Result.Status;
import com.zhan.ielts.domain.Banner;
import com.zhan.ielts.domain.query.BannerQuery;
import com.zhan.ielts.service.BannerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/banner")
public class BannerController extends BaseControllerImpl<Banner, BannerQuery> {

	private Logger log = LoggerFactory.getLogger(BannerController.class);

//	@Autowired
//	private CacheManager cacheManager;

	@Autowired
	private BannerService bannerService;

	@Override
	protected BaseService<Banner> getBaseService() {
		return bannerService;
	}

//	public CacheManager getCacheManager() {
//		return cacheManager;
//	}
//
//	public void setCacheManager(CacheManager cacheManager) {
//		this.cacheManager = cacheManager;
//	}

//	@PostConstruct
	public void init(){
		System.out.println("--------------------------------------------------------------------------");
		System.out.println("--------------------------------------------------------------------------");
		System.out.println("--------------------------------------------------------------------------");
		System.out.println("--------------------------------------------------------------------------");
		int page = 0;
		int pageSize = 5;
		Sort sort = new Sort(Direction.ASC, "priority");
		Pageable pageable = new PageRequest(page, pageSize, sort);
		Banner query = new Banner();
//		bannerList = bannerService.queryList(query, pageable);

		
		bannerService.fetchBanner(query);
		
	}
	
	
	@ResponseBody
	@RequestMapping(value = "/fetch", method = RequestMethod.POST)
	public Result fetchTreasureChest(String type) {
		log.debug("获取横幅信息开始");

//		 System.out.println(cacheManager.getCache("demoCache").get("name"));

		Map<String, Integer> map = new HashMap<String, Integer>();
		List<Banner> bannerList = null;

		int page = 0;
		int pageSize = 5;
		Sort sort = new Sort(Direction.ASC, "priority");
		Pageable pageable = new PageRequest(page, pageSize, sort);

		try {
			Banner query = new Banner();
			bannerList = bannerService.queryList(query, pageable);
		} catch (Exception e) {
			log.debug("获取横幅信息开始");
			map.put("errCode", 1001);
			return new Result(Status.ERROR, "系统错误, 获取横幅信息失败！");
		}
		log.debug("获取横幅信息结束");

		return new Result(Status.ERROR, bannerList);
	}

	@RequestMapping(value = "/addCustom", method = RequestMethod.POST)
	public ModelAndView addOne(Banner entity,
			@RequestParam(value = "imageFile", required = false) MultipartFile imageFile) {

		try {
			bannerService.updateBanner(imageFile, entity);
		} catch (Exception e) {
			return new ModelAndView(path.getRedirectListPath());
		}
		return new ModelAndView(path.getRedirectListPath());
	}

	@RequestMapping(value = "/editCustom", method = RequestMethod.POST)
	public ModelAndView editOne(Banner entity,
			@RequestParam(value = "imageFile", required = false) MultipartFile imageFile) {
		try {
			bannerService.updateBanner(imageFile, entity);
		} catch (Exception e) {
			return new ModelAndView(path.getRedirectListPath());
		}
		return new ModelAndView(path.getRedirectListPath());
	}

}
